package com.zhitan.ems.app.api.patrol;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhitan.ems.app.patrol.entity.PatrolEntity;
import com.zhitan.ems.app.patrol.entity.PatrolPointEntity;
import com.zhitan.ems.app.patrol.entity.PatrolTrackEntity;
import com.zhitan.ems.app.patrol.entity.PatrolTrackLogEntity;
import com.zhitan.ems.app.patrol.service.PatrolPointService;
import com.zhitan.ems.app.patrol.service.PatrolService;
import com.zhitan.ems.app.patrol.service.PatrolTrackLogService;
import com.zhitan.ems.app.patrol.service.PatrolTrackService;
import com.zhitan.ems.app.patrol.vo.PatrolTrackVO;
import com.zhitan.ems.app.patrol.wrapper.PatrolTrackWrapper;
import com.zhitan.ems.common.core.domain.R;
import com.zhitan.ems.common.utils.StringUtils;
import com.zhitan.ems.framework.core.vo.VPage;
import com.zhitan.ems.framework.core.vo.VQueryKit;
import com.zhitan.ems.mini.api.ApiBaseAction;
import com.zhitan.ems.mini.api.ApiHeaderVo;
import com.zhitan.ems.mini.api.ApiThreadContext;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
/**
 * 巡检轨迹
 * @author zhitan
 * 
 * @date 2024-11-02 14:05:23
 */
@Api(tags = "App管理--巡检项目--巡检轨迹", value = "巡检轨迹")
@RestController
@RequestMapping("api/patrol/patroltrack")
public class ApiPatrolTrackController extends ApiBaseAction{
    @Autowired
    private PatrolTrackService patrolTrackService;
    @Autowired
    private PatrolTrackLogService patrolTrackLogService;
    @Autowired
    private PatrolService patrolService;
    @Autowired
    private PatrolPointService patrolPointService;
  
    @ApiOperation(value = "列表", response = VPage.class)
    @ApiImplicitParams({
		        @ApiImplicitParam(name = "name", value = "标题",  paramType = "query", required = false, dataType =  "String"),
		})
    @GetMapping("/browse")
    public R<VPage<PatrolTrackVO>> browse(@RequestParam Map<String, Object> params){
        QueryWrapper<PatrolTrackEntity> queryWrapper=VQueryKit.getQueryWrapperByMap(params,Arrays.asList("name"), PatrolTrackEntity.class);
        queryWrapper.orderByDesc("id");
         queryWrapper.like(StringUtils.isNotNull(params.get("name")),"name", params.get("name"));
        IPage<PatrolTrackEntity> pages = patrolTrackService.page(VQueryKit.getPage(params), queryWrapper);
        return R.data(PatrolTrackWrapper.build().toVPageVO(pages));
    }
    /**
     * 信息
     */
    @ApiOperation(value = "信息", response = PatrolTrackVO.class)
    @GetMapping("/detail/{id}")
    public R<PatrolTrackVO> detail(@PathVariable("id") Long id){
		PatrolTrackEntity patrolTrack = patrolTrackService.getById(id);
		PatrolTrackVO patrolTrackVO=PatrolTrackWrapper.build().entityVO(patrolTrack);
		QueryWrapper<PatrolTrackLogEntity> queryWrapper=new QueryWrapper<PatrolTrackLogEntity>();
		queryWrapper.eq(VQueryKit.getUnderColumn("patrolTrackId"), id);
		queryWrapper.orderByAsc("id");
		patrolTrackVO.setPatrolTrackLogList(patrolTrackLogService.list(queryWrapper));
	    return R.data(patrolTrackVO);
    }
     /**
     * 保存&更新
     */
    @ApiOperation(value = "开始巡更", response = Boolean.class)
    @PostMapping("/submitStart")
    public R<PatrolTrackVO> submitStart(@Valid @RequestBody PatrolTrackVO patrolTrack){
   		patrolTrack.setTrackType(1);
   		PatrolEntity patrol = patrolService.getById(patrolTrack.getPatrolId());
		if(patrol!=null) {
			patrol.setTrackNum(patrol.getTrackNum()+1);
			patrol.setUpdateAt(new Date());
			patrol.setState(1);
			patrolService.updateById(patrol);
			patrolTrack.setPatrolName(patrol.getName());
		}
   		patrolTrack=trackPoint(patrolTrack);
   		if(patrolTrack!=null) {
   		  return R.data(patrolTrack);
   		}
    	return R.error("数据错误");
    }
    private PatrolTrackVO trackPoint(PatrolTrackVO patrolTrack) {
    	ApiHeaderVo apiHeaderVo=ApiThreadContext.getApiHeader();  
   		if(patrolTrack.getId()==null) {
            patrolTrack.setCreateAt(new Date());
            patrolTrack.setIsDelete(0);
        } 
   		patrolTrack.setUpdateAt(new Date());
   		patrolTrack.setUserId(apiHeaderVo.getUserId());
   		patrolTrack.setUserName(apiHeaderVo.getUserName());
   		if(patrolTrack.getPointId()!=null&&patrolTrack.getPointId()>0) {
   			PatrolPointEntity point= patrolPointService.getById(patrolTrack.getPointId());
   			if(point!=null) {
   				patrolTrack.setPointName(point.getName());
   				patrolTrack.setLatitude(point.getLatitude());
   				patrolTrack.setLongitude(point.getLongitude());
   			}
   		}
   		patrolTrackService.saveOrUpdate(patrolTrack);
    	return patrolTrack;
    }
    
    @ApiOperation(value = "结束巡更", response = Boolean.class)
    @PostMapping("/submitEnd")
    public R<PatrolTrackVO> submitEnd(@Valid @RequestBody PatrolTrackVO patrolTrack){
    	patrolTrack.setTrackType(10);
    	PatrolEntity patrol = patrolService.getById(patrolTrack.getPatrolId());
		if(patrol!=null) {
			patrol.setFinishDate(new Date());
			patrol.setTrackNum(patrol.getTrackNum()+1);
			patrol.setUpdateAt(new Date());
			patrol.setState(2);
			patrolService.updateById(patrol);
			patrolTrack.setPatrolName(patrol.getName());
		}
   		patrolTrack=trackPoint(patrolTrack);
   		if(patrolTrack!=null) {
   		  return R.data(patrolTrack);
   		}
    	return R.error("数据错误");
    }
    
    @ApiOperation(value = "扫码巡检或者直接坐标巡检", response = Boolean.class)
    @PostMapping("/submitTrack")
    public R<PatrolTrackVO> submitTrack(@Valid @RequestBody PatrolTrackVO patrolTrack){
    	patrolTrack.setTrackType(2);
    	PatrolEntity patrol = patrolService.getById(patrolTrack.getPatrolId());
		if(patrol!=null) {
			patrol.setTrackNum(patrol.getTrackNum()+1);
			patrol.setUpdateAt(new Date());
			patrol.setState(1);
			patrolService.updateById(patrol);
			patrolTrack.setPatrolName(patrol.getName());
		}
   		patrolTrack=trackPoint(patrolTrack);
   		if(patrolTrack!=null) {
   		  return R.data(patrolTrack);
   		}
    	return R.error("数据错误");
    }
//    @ApiOperation(value = "保存轨迹", response = Boolean.class)
//    @PostMapping("/submitTrackLog")
//    public R<PatrolTrackLogVO> submitTrackLog(@Valid @RequestBody PatrolTrackLogVO patrolTrackLog){
//    	ApiHeaderVo apiHeaderVo=ApiThreadContext.getApiHeader();  
//   		if(patrolTrackLog.getId()==null) {
//            patrolTrackLog.setCreateAt(new Date());
//            patrolTrackLog.setIsDelete(0);
//        } 
//   		patrolTrackLog.setUpdateAt(new Date());
//   		patrolTrackLog.setUserId(apiHeaderVo.getUserId());
//   		patrolTrackLog.setUserName(apiHeaderVo.getUserName());
//    	if(patrolTrackLogService.saveOrUpdate(patrolTrackLog)) {
//    		 return R.ok(patrolTrackLog);
//    	 }
//    	return R.error("数据错误");
//    }
}
